<html>
<head><meta charset="utf-8"><title>Understanding pointer alignment requirements · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Understanding.20pointer.20alignment.20requirements.html">Understanding pointer alignment requirements</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="209896616"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Understanding%20pointer%20alignment%20requirements/near/209896616" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tyson Nottingham <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Understanding.20pointer.20alignment.20requirements.html#209896616">(Sep 12 2020 at 19:38)</a>:</h4>
<p>I'm hazy on what kinds of pointers are valid for reads/writes with respect to alignment requirements. Can someone school me?</p>
<p>I'm pretty confident this is invalid:</p>
<div class="codehilite"><pre><span></span><code>let mut x: [u8; 8] = [0; 8];  // edit: had [u8; 4] here earlier
let p = &amp;mut x as *mut _ as *mut u64;
let value = 42;

// UB: destination may not be u64 aligned.
unsafe {
    // Write one u64 to x.
    std::ptr::write_bytes(p, value, 1);
}
</code></pre></div>


<p>But I'm less clear on these, where you're writing individual bytes into a larger integer:</p>
<div class="codehilite"><pre><span></span><code>let mut x: u64 = 0;
let p = &amp;mut x as *mut _ as *mut u8;
let value = 42;

// A: aligned for u64, but partial write
unsafe {
    std::ptr::write_bytes(p, value, 1);
}

// B: aligned for u64, but partial write and &quot;weird&quot; number of bytes
unsafe {
    std::ptr::write_bytes(p, value, 3);
}

// C: unaligned for u64 (but does that matter?) and partial write
unsafe {
    std::ptr::write_bytes(p.add(1), value, 1);
}
</code></pre></div>


<p>Are A, B, and C all valid, invalid, or a mix of both?</p>



<a name="209896721"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Understanding%20pointer%20alignment%20requirements/near/209896721" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Understanding.20pointer.20alignment.20requirements.html#209896721">(Sep 12 2020 at 19:41)</a>:</h4>
<p>The requirement is for the pointee's alignment, which is <code>u8</code> here, so they all should be fine from what I understand</p>



<a name="209896772"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Understanding%20pointer%20alignment%20requirements/near/209896772" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Understanding.20pointer.20alignment.20requirements.html#209896772">(Sep 12 2020 at 19:42)</a>:</h4>
<p>Although I think they are all implementation defined behavior because of endianness</p>



<a name="209896881"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Understanding%20pointer%20alignment%20requirements/near/209896881" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tyson Nottingham <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Understanding.20pointer.20alignment.20requirements.html#209896881">(Sep 12 2020 at 19:45)</a>:</h4>
<p><span class="user-mention" data-user-id="211727">@Jonas Schievink</span>, just to be clear, the very first example _is_ invalid though, right?</p>



<a name="209896885"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Understanding%20pointer%20alignment%20requirements/near/209896885" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Understanding.20pointer.20alignment.20requirements.html#209896885">(Sep 12 2020 at 19:45)</a>:</h4>
<p>Yeah</p>



<a name="209896904"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Understanding%20pointer%20alignment%20requirements/near/209896904" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tyson Nottingham <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Understanding.20pointer.20alignment.20requirements.html#209896904">(Sep 12 2020 at 19:46)</a>:</h4>
<p>Cool, thank you</p>



<a name="209897161"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Understanding%20pointer%20alignment%20requirements/near/209897161" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tyson Nottingham <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Understanding.20pointer.20alignment.20requirements.html#209897161">(Sep 12 2020 at 19:53)</a>:</h4>
<p><span class="user-mention" data-user-id="271719">@Mario Carneiro</span>, thanks also. I know that different endianness causes different things to happen here, but I'm assuming I can rely on it to be exactly one thing on LE and exactly another thing on BE. There's no UB I assume.</p>



<a name="209897279"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Understanding%20pointer%20alignment%20requirements/near/209897279" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Understanding.20pointer.20alignment.20requirements.html#209897279">(Sep 12 2020 at 19:56)</a>:</h4>
<p>Actually that's a good question for UCG. Is transmuting integer types specified? I'm pretty sure this is one of the areas where the zeitgeist is so strong that the spec can't really say it is anything else</p>



<a name="209897309"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Understanding%20pointer%20alignment%20requirements/near/209897309" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Understanding.20pointer.20alignment.20requirements.html#209897309">(Sep 12 2020 at 19:57)</a>:</h4>
<p>AFAIK current memory model is trying to avoid typing memory, which means that the only thing that matters is the accessing pointer's type</p>



<a name="209897349"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Understanding%20pointer%20alignment%20requirements/near/209897349" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Understanding.20pointer.20alignment.20requirements.html#209897349">(Sep 12 2020 at 19:58)</a>:</h4>
<p><a href="https://rust-lang.github.io/unsafe-code-guidelines/layout/scalars.html">https://rust-lang.github.io/unsafe-code-guidelines/layout/scalars.html</a> doesn't say anything about endianness but it does mention C compatibility, which would imply the usual behavior wrt endianness</p>



<a name="209897370"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Understanding%20pointer%20alignment%20requirements/near/209897370" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Understanding.20pointer.20alignment.20requirements.html#209897370">(Sep 12 2020 at 19:59)</a>:</h4>
<p>we will ~shortly have <code>&amp;u64 -&gt; &amp;[u8; 8]</code> in std which guarantees that, <a href="https://github.com/rust-lang/rust/pull/76610">https://github.com/rust-lang/rust/pull/76610</a></p>



<a name="209897450"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Understanding%20pointer%20alignment%20requirements/near/209897450" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Understanding.20pointer.20alignment.20requirements.html#209897450">(Sep 12 2020 at 20:01)</a>:</h4>
<p>Plus <a href="https://github.com/rust-lang/rfcs/pull/2981">https://github.com/rust-lang/rfcs/pull/2981</a>, where one of the primary motivations is looking at things as slices of bytes safely.</p>



<a name="209920870"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Understanding%20pointer%20alignment%20requirements/near/209920870" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Understanding.20pointer.20alignment.20requirements.html#209920870">(Sep 13 2020 at 08:45)</a>:</h4>
<p>OoO I did not realize <code>write_bytes</code> requires <code>dst</code> to be aligned. this is awesome!. it means that the implementation could know the alignment at compile time and potentially be faster than <code>memset</code> :)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>